package com.llxz.blog.classify.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.llxz.blog.classify.entity.Classify;
import com.llxz.blog.classify.entity.vo.One;
import com.llxz.blog.classify.service.ClassifyService;
import com.llxz.blog.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/classify")
/**
 * <p>
 * 文章类别 前端控制器
 * </p>
 *
 * @author fw
 * @since
 */
public class ClassifyController {
    @Autowired
    ClassifyService classifyService;


    @GetMapping("get/classify")
    public R getClassify() {
        List<Classify> list = classifyService.getClassify();
        return R.ok().data("data",list);
    }

    @GetMapping("list")
    public R list(){
        List<One> list = classifyService.getAllOneTwo();
        return R.ok().data("list",list);
    }

    @PostMapping("addOneClassify")
    public R addOneClassify(@RequestParam String title){
        Classify classify = new Classify();
        classify.setTitle(title);
        classify.setParentId("0");
        classifyService.save(classify);
        return R.ok().message("一级分类添加成功");
    }

    @PostMapping("addTwoClassify")
    public R addTwoClassify(@RequestParam String parentId, @RequestParam String title){
        Classify classify = new Classify();
        classify.setTitle(title);
        classify.setParentId(parentId);
        classifyService.save(classify);
        return R.ok().message("二级分类添加成功");
    }
    @DeleteMapping("removeOneClassify")
    public R removeOneClassify(@RequestParam String title){
        classifyService.removeOneClassify(title);
        return R.ok().message("删除成功");
    }
    @DeleteMapping("removeTwoClassify")
    public R removeTwoClassify(@RequestParam String title){
        QueryWrapper<Classify> wrapper = new QueryWrapper<>();
        wrapper.eq("title",title).ne("parent_id","0");
        classifyService.remove(wrapper);
        return R.ok().message("删除成功");
    }
}
